/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import core.Database;
import core.DatabaseTable;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author Sakis Vtdk <sakis.vtdk@gmail.com>
 */
public class DatabaseActionsPanel extends javax.swing.JPanel {

	/**
	 * Creates new form DatabaseActionsPanel
	 */
	public DatabaseActionsPanel() {
		initComponents();

		this.nameTextField.setText(Database.getDatabase().getDatabaseName());

		this.tablesList.setModel(createListModel());
	}

	/**
	 * Creates a ListModel containing the names of the tables of the 
	 * database
	 * 
	 * @return A ListModel containing the tables
	 */
	private DefaultListModel createListModel()
	{
		DefaultListModel listModel = new DefaultListModel();
		
		for(DatabaseTable dbTable : Database.getDatabase().getTables())
		{
			listModel.addElement(dbTable.getName());
		}

		listModel.addElement(NEW_TABLE_COMMAND);

		return listModel;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        tablesLabel = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablesList = new javax.swing.JList();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        nameLabel.setText("Name");
        add(nameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));
        add(nameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 20, 100, -1));

        tablesLabel.setText("Tables");
        add(tablesLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        submitButton.setText("Submit");
        add(submitButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 160, -1, -1));

        tablesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tablesList);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 50, 100, 100));
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton submitButton;
    private javax.swing.JLabel tablesLabel;
    private javax.swing.JList tablesList;
    // End of variables declaration//GEN-END:variables

	private final String NEW_TABLE_COMMAND = "Create new table...";

	private static Logger log = Logger.getLogger(DatabaseActionsPanel.class);
}
